package com.pica.system.service.impl;

import java.util.Date;
import java.util.List;

import com.pica.common.constant.UserConstants;
import com.pica.common.core.domain.entity.SysUser;
import com.pica.common.utils.ShiroUtils;
import com.pica.system.domain.Evaluate;
import com.pica.system.domain.EvaluateResume;
import com.pica.system.mapper.EvaluateMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.pica.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.pica.system.domain.Comments;
import com.pica.system.mapper.ResumeMapper;
import com.pica.system.domain.Resume;
import com.pica.system.service.IResumeService;
import com.pica.common.core.text.Convert;

/**
 * 简历Service业务层处理
 * 
 * @author 徐鑫杰
 * @date 2022-12-14
 */
@Service
public class ResumeServiceImpl implements IResumeService 
{
    @Autowired
    private ResumeMapper resumeMapper;
    @Autowired
    private EvaluateMapper evaluateMapper;

    /**
     * 查询简历
     * 
     * @param resId 简历主键
     * @return 简历
     */
    @Override
    public Resume selectResumeByResId(Long resId)
    {
        return resumeMapper.selectResumeByResId(resId);
    }

    /**
     * 查询简历列表
     * 
     * @param resume 简历
     * @return 简历
     */
    @Override
    public List<Resume> selectResumeList(Resume resume)
    {
        return resumeMapper.selectResumeList(resume);
    }

    /**
     * 新增简历
     * 
     * @param resume 简历
     * @return 结果
     */
    @Transactional
    @Override
    public int insertResume(Resume resume)
    {
        int rows = resumeMapper.insertResume(resume);
        insertComments(resume);
        return rows;
    }

    /**
     * 修改简历
     * 
     * @param resume 简历
     * @return 结果
     */
    @Transactional
    @Override
    public int updateResume(Resume resume)
    {
        resumeMapper.deleteCommentsByResId(resume.getResId());
        insertComments(resume);
        return resumeMapper.updateResume(resume);
    }



    /**
     * 简历反馈的service
     *
     * @param evaluateresume 简历
     * @return 结果
     */
    @Transactional
    @Override
    public int updateEvaluateResume(EvaluateResume evaluateresume)
    {
        //分配表单提交的数据
        Resume resume=new Resume();
        resume.setResId(evaluateresume.getResId());
        resume.setResStatus(evaluateresume.getResStatus());
        resume.setResBan(evaluateresume.getResBan());
        resume.setResJob(evaluateresume.getResJob());

        Evaluate evaluate=new Evaluate();
        evaluate.setEvaId(evaluateresume.getResId());
        evaluate.setEavAbility1(evaluateresume.getEavAbility1());
        evaluate.setEavAbility2(evaluateresume.getEavAbility2());
        evaluate.setEavAbility3(evaluateresume.getEavAbility3());
        evaluate.setEavAbility4(evaluateresume.getEavAbility4());
        evaluate.setEavAbility5(evaluateresume.getEavAbility5());
        evaluate.setEavAbility6(evaluateresume.getEavAbility6());
        evaluate.setEavAbility7(evaluateresume.getEavAbility7());
        evaluate.setEvaIs1(evaluateresume.getEvaIs1());
        evaluate.setEvaIs2(evaluateresume.getEvaIs2());
        evaluate.setEvaIs3(evaluateresume.getEvaIs3());
        evaluate.setEvaIs4(evaluateresume.getEvaIs4());
        resume.setResId(evaluateresume.getResId());

        resumeMapper.updateResume(resume);
        evaluateMapper.updateEvaluate(evaluate);

        return 2;
    }

    /**
     * 批量删除简历
     * 
     * @param resIds 需要删除的简历主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteResumeByResIds(String resIds)
    {
        resumeMapper.deleteCommentsByResIds(Convert.toStrArray(resIds));
        return resumeMapper.deleteResumeByResIds(Convert.toStrArray(resIds));
    }

    /**
     * 删除简历信息
     * 
     * @param resId 简历主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteResumeByResId(Long resId)
    {
        resumeMapper.deleteCommentsByResId(resId);
        return resumeMapper.deleteResumeByResId(resId);
    }

    /**
     * 校验手机号码是否唯一
     *
     * @param user 用户信息
     * @return
     */
    @Override
    public String checkPhoneUnique(Resume resume)
    {
        Long userId = StringUtils.isNull(resume.getResId()) ? -1L : resume.getResId();
        Resume info = resumeMapper.checkPhoneUnique(resume.getResTel());
        if (StringUtils.isNotNull(info) && info.getResId().longValue() != userId.longValue())
        {
            return UserConstants.USER_PHONE_NOT_UNIQUE;
        }
        return UserConstants.USER_PHONE_UNIQUE;
    }

    /**
     * 新增comments信息
     * 
     * @param resume 简历对象
     */
    public void insertComments(Resume resume)
    {
        List<Comments> commentsList = resume.getCommentsList();
        Long resId = resume.getResId();
        if (StringUtils.isNotNull(commentsList))
        {
            List<Comments> list = new ArrayList<Comments>();
            for (Comments comments : commentsList)
            {
                comments.setResId(resId);
                comments.setCu(ShiroUtils.getSysUser().getUserId());
                comments.setUu(ShiroUtils.getSysUser().getUserId());
                comments.setCt(new Date());
                comments.setUt(new Date());
                list.add(comments);
            }
            if (list.size() > 0)
            {
                resumeMapper.batchComments(list);
            }
        }
    }
}
